package cn.idietrade.idietradegoodsorder8003.demo.controller;

import cn.idietrade.common.pojo.SecondOrder;
import cn.idietrade.common.pojo.SecondUser;
import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.common.utils.SecondOrderPage;
import cn.idietrade.idietradegoodsorder8003.demo.service.GoodsOrderService;
import cn.idietrade.idietradegoodsorder8003.demo.service.OrderService;
import cn.idietrade.idietradegoodsorder8003.demo.service.PayService;
import cn.idietrade.idietradegoodsorder8003.demo.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者：李永康
 * @createTime 2022/11/30 14:49
 * @description 描述
 */
@RestController
public class GoodsOrderController {

    @Resource
    private GoodsOrderService goodsOrderService;
    @Resource
    private OrderService orderService;
    @Resource
    private RedisUtil redisUtil;



    //查询订单
    @RequestMapping("/getOrderList")
    public  ReturnResultData queryOrderList(Integer buyerId,Integer sellerId,Integer orderNo){
        ReturnResultData resultData = new ReturnResultData();
        resultData.setData(goodsOrderService.orderList(buyerId, sellerId, orderNo));
        return  resultData;
    }

    //往redis存储有生存时间的订单
    @RequestMapping("/createOrderRedis")
    public ReturnResultData createOrderRedis(Integer userId,String goodsId,Integer goodsNum){
        ReturnResultData resultData =new ReturnResultData<>();
        ReturnResultData orderRedis = orderService.createOrderRedis(userId, goodsId, goodsNum);
        if (!EmptyUtils.isEmpty(orderRedis.getCode()>0)){
            resultData.setMessage("临时订单生成成功");
            resultData.setData(null);
            resultData.setCode(1);
        }else {
            resultData.setMessage("数据异常，请和管理员联系");
            resultData.setData(null);
            resultData.setCode(-1);
        }
        return resultData;
    }




    //生成订单固化到mysql
    @RequestMapping("/createOrder")
    public ReturnResultData createOrder(String token,String orderId,String payType){
        ReturnResultData resultData =new ReturnResultData<>();
        SecondUser user = JSONObject.parseObject(redisUtil.getStr(token), SecondUser.class);
        if (!EmptyUtils.isEmpty(user)){
            if (user.getUserPermission() == 0){
                resultData.setMessage("权限不足，请向管理员申请权限");
                resultData.setData(null);
                resultData.setCode(-1);
                return resultData;
            }
            if(EmptyUtils.isEmpty(orderId)){
                resultData.setMessage("数据异常，请与管理员联系，订单id为空");
                resultData.setData(null);
                resultData.setCode(-1);
                return resultData;
            }
            String[] orderIds = orderId.split(",");
            List<Integer> orderIdList = new ArrayList<>();
            for (String ids : orderIds) {
                orderIdList.add(Integer.valueOf(ids));
            }
            List<SecondOrder> orderList = new ArrayList<>();
            SecondOrder secondOrder=null;
            for (Integer order : orderIdList) {
                secondOrder = JSON.parseObject(redisUtil.getStr(order.toString()), SecondOrder.class);
                secondOrder.setPayType(Integer.valueOf(payType));
                orderList.add(secondOrder);
            }
            ReturnResultData order = orderService.createOrder(orderList);
            int updateGoodsNum = goodsOrderService.updateGoodsNum(secondOrder.getGoodsId(),secondOrder.getGoodsNum());
            if(order.getCode()>0&&updateGoodsNum>0){
                resultData.setMessage("订单生成成功");
                resultData.setData(null);
                resultData.setCode(1);
            }
        }else {
            resultData.setMessage("数据异常，请和管理员联系");
            resultData.setData(null);
            resultData.setCode(-1);
        }
        return resultData;
    }

    //取消订单内部分商品
    @RequestMapping("/delOrderGoodsInfo")
    public ReturnResultData delOrderGoodsInfo(String token,Integer goodsId){
        ReturnResultData resultData =new ReturnResultData<>();
        SecondUser user =JSON.parseObject(redisUtil.getStr(token), SecondUser.class);
        if (!EmptyUtils.isEmpty(user)){
            if (user.getUserPermission() == 0){
                resultData.setMessage("权限不足，请向管理员申请权限");
                resultData.setData(null);
                resultData.setCode(-1);
                return resultData;
            }
            return orderService.removeOrderGoodsInfo(goodsId);
        }else {
            resultData.setMessage("数据异常，请和管理员联系");
            resultData.setData(null);
            resultData.setCode(-1);
        }
        return resultData;
    }

    //修改订单支付状态
    @RequestMapping("/updateOrderState")
    public ReturnResultData updateOrderState(String token,Integer orderState,String orderNo){
        ReturnResultData resultData =new ReturnResultData<>();
        SecondUser user =JSON.parseObject(redisUtil.getStr(token), SecondUser.class);
        if (!EmptyUtils.isEmpty(user)){
            if (user.getUserPermission() == 0){
                resultData.setMessage("权限不足，请向管理员申请权限");
                resultData.setData(null);
                resultData.setCode(-1);
                return resultData;
            }
            return orderService.updateOrderState(orderState,orderNo);
        }else {
            resultData.setMessage("数据异常，请和管理员联系");
            resultData.setData(null);
            resultData.setCode(-1);
        }
        return resultData;
    }

    @RequestMapping("/queryRedisLSOrder")
    public ReturnResultData<List<SecondOrder>> queryRedisLSOrder(Integer userId){
        ReturnResultData<List<SecondOrder>> resultData = new ReturnResultData<>();
        SecondOrder secondOrder = orderService.queryRedisLSOrder(userId);
        List<SecondOrder> list =new ArrayList<>();
        if(EmptyUtils.isNotEmpty(secondOrder)){
            list.add(secondOrder);
            resultData.setCode(1);
            resultData.setMessage("临时订单查询成功");
            resultData.setData(list);
        }else{
            resultData.setCode(0);
            resultData.setMessage("临时订单查询失败");
            resultData.setData(null);
        }
        return  resultData;
    }


    @RequestMapping("/querySecondOrders")
    public ReturnResultData<SecondOrderPage> querySecondOrders(Integer pageNum,Integer pageSize,Integer userId){
        ReturnResultData<SecondOrderPage> resultData =new ReturnResultData<>();
        try{
            SecondOrderPage orderList = orderService.querySecondOrders(pageNum, pageSize, userId);
            if(EmptyUtils.isNotEmpty(orderList)){
                resultData.setCode(1);
                resultData.setMessage("所有订单+所有订单下的商品查询成功");
                resultData.setData(orderList);
            }else{
                resultData.setCode(0);
                resultData.setMessage("所有订单+所有订单下的商品查询失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setCode(0);
            resultData.setMessage("所有订单+所有订单下的商品查询数据异常");
            resultData.setData(null);
        }
        return resultData;
    }

}
